package com.ossean.projectmanager.lasttabledao;

import java.util.List;

import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.ossean.projectmanager.model.OpenSourceProject;
import com.ossean.projectmanager.model.Taggings;

public interface OpenSourceProjectDao {

	// 向tag表存储数据
	@Insert("insert ignore into ${table} (name) values (#{name})")
	public void insertTag(@Param("table") String table,
			@Param("name") String name);

	// 根据tag的name找tag的id
	@Select("select id from ${table} where name=#{name}")
	public int selectTagIdByName(@Param("table") String table,
			@Param("name") String name);

	// 插入Taggings对象
	@Insert("insert ignore into ${table} (tag_id,taggable_id,taggable_type,context,created_at,disagree_num) values (#{model.tag_id},#{model.taggable_id},#{model.taggable_type},#{model.context},#{model.created_at},#{model.disagree_num})")
	public void insertTaggings(@Param("table") String table,
			@Param("model") Taggings model);

	// 获得最大项目id
	@Select("select MAX(id) from open_source_projects")
	public Integer getNewLast();

	// 读取一定数量的项目信息
	@Select("select * from open_source_projects where id>=#{start} limit #{size}")
	public List<OpenSourceProject> getProjectsByBatch(
			@Param("start") int start, @Param("size") int size);

	// 更新项目标签字段tags和权重更高的标签字段tags_for_search
	@Update("update open_source_projects set tags=#{tags}, tags_for_search = #{tagsForSearch} where id=#{id}")
	public void updatePrjTags(@Param("id") int id, @Param("tags") String tags,
			@Param("tagsForSearch") String tagsForSearch);

	// 对项目标签属性进行更新
	@Update("update open_source_projects set tags=#{tags} where id=#{id}")
	public void updateTagsOfProject(@Param("id") int id,
			@Param("tags") String tags);

	// 批量获取项目
	@Select("select id,source,url,filtration from open_source_projects where filtration=0 and id >= #{startId} limit #{batchSize}")
	public List<OpenSourceProject> getBatchPrjs(@Param("startId") int startId,
			@Param("batchSize") int batchSize);
	
	// 根据开始和结束位置批量获取项目
	@Select("select id,source,url,filtration from open_source_projects where filtration=0 and id >= #{startId} and id <= #{endId}")
	public List<OpenSourceProject> getPrjsByBorder(@Param("startId") int startId,
			@Param("endId") int endId);
	@Select("select min(id) from open_source_projects where filtration=#{filtration}")
	public int getMinId(@Param("filtration") int filtration);
	@Select("select max(id) from open_source_projects where filtration=#{filtration}")
	public int getMaxId(@Param("filtration") int filtration);
	// 删除项目
	@Update("delete from  open_source_projects where id=#{id}")
	public void deleteProject(@Param("id") int id);

	// filtration为1表示保留，为2表示之前保留的且已处理，为0表示不保留
	@Update("update open_source_projects set filtration = #{filtration} where id = #{prjId}")
	public void updateFiltratedPrj(@Param("prjId") int prjId,
			@Param("filtration") int filtration);

	@Update("UPDATE open_source_projects SET tags = ''")
	public void emptyOspTags();
}
